"""
Case Type   : bugfix
Case Name   : 校验timezone默认值
Create At   : 2025/6/16
Owner       : liu-tong-8848
Description :
    1、查询安装好的主备数据库时区
    2、将数据库时区置为默认值并重启数据库
    3、查询时区的默认值
    4、清理环境
Expect      :
    1、时区为PRC
    2、成功
    3、时区为GMT
    4、成功
History     :
"""

import os
import unittest

from yat.test import Node
from yat.test import macro

from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger


class BugFix(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.common = Common()
        self.sh_primary = CommonSH('PrimaryDbUser')
        self.user_node = Node('PrimaryDbUser')
        self.constant = Constant()

    def test_dolphin(self):
        self.log.info(f'----{os.path.basename(__file__)} start----')
        text = '----step1:查询安装好的主备数据库时区；expect:时区为PRC----'
        self.log.info(text)
        sql_cmd = f'show time zone;'
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd)
        self.log.info(sql_res)
        self.assertIn('PRC', sql_res, '执行失败' + text)

        text = '----step2:将数据库时区置为默认值并重启数据库；expect:成功----'
        self.log.info(text)
        result = self.sh_primary.execute_gsguc("reload", self.constant.GSGUC_SUCCESS_MSG, "TimeZone")
        self.log.info(result)
        self.assertTrue(result)
        result = self.sh_primary.restart_db_cluster()
        self.log.info(result)
        self.assertTrue(result)

        text = '----step3:查询时区的默认值；expect:时区为GMT----'
        self.log.info(text)
        sql_cmd = f'show time zone;'
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd)
        self.log.info(sql_res)
        self.assertIn('GMT', sql_res, '执行失败' + text)

    def tearDown(self):
        text = '----step4:清理环境；expect:成功----'
        self.log.info(text)
        result = self.sh_primary.execute_gsguc("reload", self.constant.GSGUC_SUCCESS_MSG, "TimeZone = 'PRC'")
        self.log.info(result)
        self.assertTrue(result)
        result = self.sh_primary.restart_db_cluster()
        self.log.info(result)
        self.assertTrue(result)
        sql_cmd = f'show time zone;'
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd)
        self.log.info(sql_res)
        self.assertIn('PRC', sql_res, '执行失败' + text)
        self.log.info(f'----{os.path.basename(__file__)} end-----')
